unit untControleAcesso;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ComCtrls, Mask, DBCtrls, DB, {SimpleDS,}
  ImgList, ExtCtrls, IBCustomDataSet;

type
  TfrmControleAcesso = class(TForm)
    TreeView: TTreeView;
    btnPesquisa: TBitBtn;
    btnGravar: TSpeedButton;
    btnVoltar: TSpeedButton;
    lbl1: TLabel;
    edtLogin: TDBEdit;
    lbl2: TLabel;
    edtCodigo: TDBEdit;
    il1: TImageList;
    dsUsuario: TDataSource;
    procedure btnPesquisaClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnVoltarClick(Sender: TObject);
    procedure DesenhaTreeView(tabela: TIBDataSet; TV:TTreeView);
    {procedure DesenhaTreeView(tabela: TSimpleDataSet; TV:TTreeView);}
    procedure TreeViewKeyPress(Sender: TObject; var Key: Char);
    procedure btnGravarClick(Sender: TObject);
    procedure TreeViewDblClick(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmControleAcesso: TfrmControleAcesso;

implementation

uses untDModule, untPesquisaUsuario, untMenuPrincipal;

{$R *.dfm}


procedure TfrmControleAcesso.btnPesquisaClick(Sender: TObject);
begin
  Application.CreateForm(TfrmPesquisaUsuario, frmPesquisaUsuario);
  frmPesquisaUsuario.ShowModal;
  freeAndNil(frmPesquisaUsuario);
end;

procedure TfrmControleAcesso.FormShow(Sender: TObject);
begin
  DM.dstUsuario.Close;
  TreeView.Items.Clear;
end;

procedure TfrmControleAcesso.btnVoltarClick(Sender: TObject);
begin
  close;
end;

procedure TfrmControleAcesso.DesenhaTreeView(Tabela: TIBDataSet; TV:TTreeView);
var
  posicao, i, ix, iz, iw, nPos: integer;
  MyTreeNode1, MyTreeNode2, MyTreeNode3, MyTreeNode4: TTreeNode;
  xTexto: string;
begin
  inherited;
  with TV.Items do
      begin
          Clear;
          npos := 1;
          for i := 0 to frmMenuPrincipal.MenuPrincipal.items.count - 1 do
            begin
              if frmMenuPrincipal.MenuPrincipal.items[i].Caption <> '-' then
                begin
                  Posicao := Pos('&', frmMenuPrincipal.MenuPrincipal.Items[i].Caption);
                  if Posicao = 0 then
                    xTexto := frmMenuPrincipal.MenuPrincipal.Items[i].Caption
                  else
                    xTexto := Copy(frmMenuPrincipal.MenuPrincipal.Items[i].Caption, 1, Posicao - 1) +
                      Copy(frmMenuPrincipal.MenuPrincipal.Items[i].Caption, Posicao + 1, 100);
                  MyTreeNode1 := Add(nil, xTexto);
                  MyTreeNode1.ImageIndex := 0;
                  MyTreeNode1.SelectedIndex := 0;
                  if not tabela.isEmpty then
                    begin
                      if Copy(tabela.FieldByName('NIVEL').AsString, nPos, 1) = 'S' then
                        MyTreeNode1.StateIndex := 2
                      else
                        MyTreeNode1.StateIndex := 1;
                      nPos := nPos + 1;
                      for ix := 0 to frmMenuPrincipal.MenuPrincipal.items[i].count - 1 do
                        begin
                          if frmMenuPrincipal.MenuPrincipal.items[i].items[ix].caption <> '-' then
                            begin
                              Posicao := Pos('&', frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].Caption);
                              if Posicao = 0 then
                                xTexto := frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].Caption
                              else
                                xTexto := Copy(frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].Caption, 1, Posicao - 1) +
                                  Copy(frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].Caption, Posicao + 1, 100);
                              MyTreeNode2 := AddChild(MyTreeNode1, xTexto);
                              MyTreeNode2.ImageIndex := 1;
                              MyTreeNode2.SelectedIndex := 1;
                              if Copy(tabela.FieldByName('NIVEL').AsString, nPos, 1) = 'S' then
                                MyTreeNode2.StateIndex := 2
                              else
                                MyTreeNode2.StateIndex := 1;
                              nPos := nPos + 1;
                              for iz := 0 to frmMenuPrincipal.MenuPrincipal.items[i].items[ix].count - 1 do
                                begin
                                  if frmMenuPrincipal.MenuPrincipal.items[i].items[ix].items[iz].caption <> '-' then
                                    begin
                                      Posicao := Pos('&', frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].items[iz].Caption);
                                      if Posicao = 0 then
                                        xTexto := frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].items[iz].Caption
                                      else
                                        xTexto := Copy(frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].items[iz].Caption, 1, Posicao -
                                          1)
                                          +
                                          Copy(frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].items[iz].Caption, Posicao + 1, 100);
                                      MyTreeNode3 := AddChild(MyTreeNode2, xTexto);
                                      MyTreeNode3.ImageIndex := 2;
                                      MyTreeNode3.SelectedIndex := 2;
                                      if Copy(tabela.FieldByName('NIVEL').AsString, nPos, 1) = 'S' then
                                        MyTreeNode3.StateIndex := 2
                                      else
                                        MyTreeNode3.StateIndex := 1;
                                      nPos := nPos + 1;
                                      for iw := 0 to frmMenuPrincipal.MenuPrincipal.items[i].items[ix].Items[iz].count - 1 do
                                        begin
                                          if frmMenuPrincipal.MenuPrincipal.items[i].items[ix].items[iz].Items[iw].caption <> '-' then
                                          begin
                                            Posicao := Pos('&', frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].items[iz].Items[iw].Caption);
                                            if Posicao = 0 then
                                              xTexto := frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].items[iz].Items[iw].Caption
                                            else
                                              xTexto := Copy(frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].items[iz].Items[iw].Caption, 1, Posicao - 1)+
                                              Copy(frmMenuPrincipal.MenuPrincipal.Items[i].items[ix].items[iz].Items[iw].Caption, Posicao + 1, 100);
                                              MyTreeNode4 := AddChild(MyTreeNode2, xTexto);
                                              MyTreeNode4.ImageIndex := 2;
                                              MyTreeNode4.SelectedIndex := 2;
                                              if Copy(tabela.FieldByName('NIVEL').AsString, nPos, 1) = 'S' then
                                              MyTreeNode4.StateIndex := 2
                                          else
                                            MyTreeNode4.StateIndex := 1;
                                            nPos := nPos + 1;
                                          end;
                                        end;
                                    end;
                                end;
                            end;
                        end;
                    end;
                end;
            end;
        end;
end;

procedure TfrmControleAcesso.TreeViewKeyPress(Sender: TObject; var Key: Char);
var
  TreeNod: TTreeNode;
begin
  {if Key = #32 then
  begin
    TreeNod := TreeView.Selected;

    if TreeNod.StateIndex = 1 then
    begin
      TreeNod.StateIndex := 2;
    end
    else
    begin
      TreeNod.StateIndex := 1;
    end;
  end;
  }
end;

procedure TfrmControleAcesso.btnGravarClick(Sender: TObject);
var
  I: integer;
  TreeNod: TTreeNode;
  Permissao: string;
begin
  Permissao := '';
  for i := 0 to TreeView.Items.Count - 1  do
  begin
   TreeNod := TreeView.Items[I];
   if TreeNod.StateIndex = 1 then
   begin
     Permissao := Permissao + 'N';
   end
   else
   begin
     Permissao := Permissao + 'S';
   end;
  end;

  DM.dstUsuario.Edit;
  DM.dstUsuarioNIVEL.Value := Permissao;
  DM.dstUsuario.Post;
  dm.tBanco.CommitRetaining;
  MessageBox (Handle, 'Controle de Acesso Cadastrado com sucesso!', 'Aviso', MB_ICONWARNING);
  Close;
end;

procedure TfrmControleAcesso.TreeViewDblClick(Sender: TObject);
var
  TreeNod: TTreeNode;
begin
    TreeNod := TreeView.Selected;

    if TreeNod.StateIndex = 1 then
    begin
      TreeNod.StateIndex := 2;
    end
    else
    begin
      TreeNod.StateIndex := 1;
    end;
end;

end.
